import { LoveTaskBaseBody } from "./LoveTaskBaseBody";
import { Cache } from "../../cache/Cache";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { DataEvent } from "../../events/DataEvent";
import { EventName } from "../../mvc/EventName";
import { MsgManager } from "../../manager/MsgManager";
import { Language } from "../../../../extend/language/Language";
import { SecTimer } from "../../../../com/gengine/core/frame/SecTimer";
import { TimerType } from "../../../../com/gengine/core/frame/TimerType";
import { LoveTaskPlayerPanel } from "./findPanel/LoveTaskPlayerPanel";
import { ObjectPool } from "../../../../com/gengine/utils/pools/ObjectPool";
import { GImageBitmap } from "../../../../com/mui/controls/GImageBitmap";
import { ImagesConst } from "../../resource/ImagesConst";
import { ObjCreate } from "../common/ObjCreate";
import { GBox } from "../../../../com/mui/containers/GBox";
import { GBoxDirection } from "../../../../com/mui/containers/globalVariable/GBoxDirection";
import { FilterConst } from "../../../component/gconst/FilterConst";
import { LoveTaskInviteInfo } from "../../model/loveTask/LoveTaskInviteInfo";
import { GLoadedButton } from "../../../../com/mui/controls/GLoadedButton";
type int = number;
//class LoveTaskFindPanel
    
    export  class LoveTaskFindPanel extends LoveTaskBaseBody
    {
        constructor()
        {
            super();
            return;
        }

        /* internal  */onFindBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (Cache.instance.loveTask.freeFlushCount <= 0) 
            {
                if (Cache.instance.role.money.coin + Cache.instance.role.money.coinBind < 1000) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallAutoBuyCoin, 1000 - (Cache.instance.role.money.coin + Cache.instance.role.money.coinBind)));
                    MsgManager.showRollTipsMsg(Language.getString(20038));
                    return;
                }
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.LoveTaskFindReq));
            this.timerStart();
            return;
        }

        /* internal  */timerStart(): void
        {
            this._findBtn.enabled = false;
            if (!this._timer) 
            {
                this._timer = new SecTimer(1, 3);
                this._timer.addListener(TimerType.ENTERFRAME, this.onTimerEnterHandler);
                this._timer.addListener(TimerType.COMPLETE, this.onTimerComHandler);
            }
            this._timer.reset();
            if (!this._timer.running) 
            {
                this._timer.start();
            }
            return;
        }

        /* internal  */onTimerEnterHandler(arg1: SecTimer): void
        {
            this._findBtn.label = "（" + arg1.repeatCount + "）";
            return;
        }

        /* internal  */onTimerComHandler(arg1: SecTimer): void
        {
            this._timer.stop();
            this._findBtn.enabled = true;
            this._findBtn.label = Language.getString(21220);
            return;
        }

        /* internal  */clearPlayerList(): void
        {
            var loc1=null;
            while (this._playerList.numChildren > 0) 
            {
                loc1 = this._playerList.getChildAt(0) as LoveTaskPlayerPanel;
                loc1.dispose();
                ObjectPool.disposeObject(loc1, LoveTaskPlayerPanel);
                this._playerList.removeChild(loc1);
            }
            return;
        }

        protected /* override */ initUI(): void
        {
            super.initUI();
            addChild(new GImageBitmap(ImagesConst.LoveFindBg, 0, 0));
            _textFormat.color = 16749567;
            _textFormat.align = flash.text.TextFormatAlign.CENTER;
            _titleTxt = ObjCreate.createTextField(Language.getString(21224), 0, 20, this.width, 20, this, _textFormat);
            _titleTxt.textColor = 16749567;
            this._playerList = new GBox();
            this._playerList.direction = GBoxDirection.HORIZONTAL;
            this._playerList.x = 0;
            this._playerList.y = 0;
            addChild(this._playerList);
            this._findBtn = ObjCreate.createGLoadedButton("MarryBtn", 0, 0, 230, 39, this);
            this._findBtn.label = Language.getString(21220);
            this._findBtn.textField.filters = [FilterConst.nameGlowFilter];
            this._findBtn.x = (this.width - this._findBtn.width) / 2;
            this._findBtn.y = this.height - 50;
            this._findBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onFindBtnClickHandler);
            setMarryBtnStyle(this._findBtn);
            _textFormat.align = flash.text.TextFormatAlign.LEFT;
            this._findTxt = ObjCreate.createTextField("", this._findBtn.x, this._findBtn.y + this._findBtn.height - 7, 220, 20, this, _textFormat);
            this._findTxt.textColor = 65280;
            this._findTxt.mouseEnabled = false;
            addChild(new GImageBitmap(ImagesConst.LoveGirl, -220, 0));
            addChild(new GImageBitmap(ImagesConst.LoveBoy, this.width - 40, 0));
            return;
        }

        public /* override */ updateData(arg1: Array<any>=null): void
        {
            var loc1=0;
            var loc3=null;
            var loc4=null;
            super.updateData(arg1);
            var loc2=arg1.length;
            while (loc1 < loc2) 
            {
                loc3 = ObjectPool.getObject(LoveTaskPlayerPanel);
                if (typeof arg1[loc1] === "lovetaskinviteinfo") 
                {
                    loc4 = arg1[loc1];
                    loc3.updateData(loc4);
                }
                this._playerList.addChild(loc3);
                ++loc1;
            }
            this._playerList.invalidate();
            this._playerList.x = (this.width - loc2 * 138) / 2;
            return;
        }

        public /* override */ updateFreeCount(): void
        {
            if (Cache.instance.loveTask.freeFlushCount > 0) 
            {
                this._findTxt.text = Language.getStringByParam(21221, Cache.instance.loveTask.freeFlushCount + "/" + Cache.instance.loveTask.freeFlushCountTotal);
                if (Cache.instance.vip.isVIP) 
                {
                    this._findTxt.appendText("(VIP)");
                }
            }
            else 
            {
                this._findTxt.text = Language.getString(21222);
            }
            this._findTxt.x = this._findBtn.x + (this._findBtn.width - this._findTxt.textWidth) / 2;
            return;
        }

        public /* override */ dispose(): void
        {
            this.clearPlayerList();
            super.dispose();
            return;
        }

        public /* override */ get bgW(): int
        {
            return 700;
        }

        public /* override */ get bgH(): int
        {
            return 380;
        }

        public /* override */ get width(): number
        {
            return 642;
        }

        public /* override */ get height(): number
        {
            return 320;
        }

        protected /* var */_playerList: GBox;

        protected /* var */_findBtn: GLoadedButton;

        protected /* var */_findTxt: Object /* flash.text.TextField */;

        protected /* var */_timer: SecTimer;
    }
